package com.share.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.domain.Angle;
import com.share.domain.Content;
import com.share.domain.Lexicon;
import com.share.mapper.AngleMapper;
import com.share.mapper.LexiconMapper;
import com.share.service.ContentService;
import com.share.mapper.ContentMapper;
import com.share.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content>
    implements ContentService{

    @Autowired
    LexiconMapper lexiconMapper;

    @Autowired
    AngleMapper angleMapper;

    public Content getInfoById(Long id) {
        Content content = this.getById(id);
        Lexicon lexicon = lexiconMapper.selectById(content.getLexId());
        Angle angle = angleMapper.selectById(content.getAid());
        content.setLexName(lexicon.getName());
        content.setAngleName(angle.getName());

        return content;
    }


    public Page<Content> findPage(Content content, Page page) {
        QueryWrapper<Content> queryWrapper = new QueryWrapper<>();
        if (content.getLexId()!=null){
            queryWrapper.eq("lex_id",content.getLexId());
        }
        if (content.getAid()!=null){
            queryWrapper.eq("aid",content.getAid());
        }
        if (StrUtil.isNotBlank(content.getBodys())){
            queryWrapper.like("bodys",content.getBodys());
        }

        Page<Content> contentPage = this.page(page,queryWrapper);
        List<Content> records = contentPage.getRecords().stream().map(con -> {
            Lexicon lexicon = lexiconMapper.selectById(con.getLexId());
            Angle angle = angleMapper.selectById(con.getAid());
            con.setLexName(lexicon.getName());
            con.setAngleName(angle.getName());
            return con;
        }).collect(Collectors.toList());
        contentPage.setRecords(records);

        return contentPage;
    }
}




